package com.imc.api.order.controller;

import com.github.pagehelper.PageInfo;
import com.imc.api.attence.pojo.Lesson;
import com.imc.api.order.pojo.OrderBillDto;
import com.imc.api.order.service.IOrderService;
import com.imc.api.order.service.WxOrderService;
import com.imc.core.config.feign.MyPageImpl;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.user.pojo.User;
import com.twp.common.component.process.exception.combo.IComboExecptionForTFunc;
import com.twp.common.tuple.ResultDTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;
import java.util.stream.Collectors;

/*
 * 类描述：微信订单控制类
 * 开发者：tanweiping
 * 创建日期：2018/10/28 21:10
 */
@RestController
@RequestMapping("/api/order")
public class WxOrderController {

    @Autowired
    IOrderService orderService;//订单服务类

    @Autowired
    IComboExecptionForTFunc iComboExecptionForTFunc;

    @Autowired
    WxOrderService wxOrderService;//微信订单服务类

    @Autowired
    MysqlUtil mysqlUtil;

    @GetMapping("getAvailableSchedule")
    public ResultDTO getAvailableSchedule(String orderDetailId){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t->{
                    t.setData(wxOrderService.getAvailableSchedule(orderDetailId));
                },
                (e,t)->{
                    t.setIsSuccess(false);
                    t.setMessage("获取订单课时失败");
                }
        );
    }

    /*
     * YT  2018/10/28 21:11
     * 方法描述：获取订单列表
     */
    @GetMapping("")
    ResultDTO getOrderList(
            Principal principal,
            @PageableDefault(size = 4, sort = {"orderDate"}, direction = Sort.Direction.DESC) Pageable pageable,
            @RequestParam(defaultValue = "all") String status,
            @RequestParam(defaultValue = "") String option
    ) {
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    t.setData(wxOrderService.getOrderList(principal,pageable,status,option));
                },
                (e, t) -> {
                    t.setIsSuccess(false);
                    t.setMessage("获取订单失败");
                }
        );
    }

    /*
     * tanweiping  2018/9/26 09:44
     * 方法描述：立即下单
     */
//    @PostMapping("")
//    ResultDTO postOrder(Principal principal, @RequestBody Lesson lesson,@RequestParam(defaultValue = "1")int num) {
//        return iComboExecptionForTFunc
//                .executeFlow(
//                        ResultDTO.builder().build(),
//                        t -> {
//                            try {
//                                User user = mysqlUtil.getUserByLoginName(principal.getName());
//                                t.setData(wxOrderService.postOrder(user, lesson,num));
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                                t.setIsSuccess(false);
//                                t.setMessage(e.getMessage());
//                            }
//                        },
//                        (e, t) -> {
//                            e.printStackTrace();
//                            t.setIsSuccess(false);
//                            t.setMessage("订单下单失败");
//                        }
//                );
//    }

    /*
     * tanweiping  2018/9/28 18:55
     * 方法描述：提供购物车id列表下单
     */
    @PostMapping("/prepayForMall")
    ResultDTO prepayForMall(Principal principal, @RequestBody List <String> ids) {
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    if (!ids.isEmpty()) {
                        List <Integer> ids_ = ids.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());
                        try {
                            t.setData(wxOrderService.prepayForMall(principal, ids_));
                        } catch (Exception e) {
                            e.printStackTrace();
                            t.setIsSuccess(false);
                            t.setMessage(e.getMessage());
                        }
                        return;
                    }
                    t.setIsSuccess(false);
                    t.setMessage("下单课程不可为空");
                },
                (e, t) -> {
                    t.setIsSuccess(false);
                    t.setMessage("下单失败");
                }
        );
    }

    @GetMapping("/queryOrderBill")
    @ResponseBody
    public ResultDTO queryOrderBill(
            Principal principal,
            String orderType,
            @RequestParam(defaultValue = "")String orderStatus,
            @RequestParam(defaultValue = "")String goodsStatus,
            @RequestParam(defaultValue = "")String orderId,
            @PageableDefault Pageable pageable,
            HttpServletRequest request){
        return iComboExecptionForTFunc
                .executeFlow(
                        ResultDTO.builder().build(),
                        t -> {
                            User user= mysqlUtil.getUserByLoginName(principal.getName());
                            OrderBillDto orderBillDto=OrderBillDto.builder()
                                    .orderType(orderType)
                                    .orderStatus(orderStatus)
                                    .goodsStatus(goodsStatus)
                                    .orderId(orderId)
                                    .size(pageable.getPageSize())
                                    .page(pageable.getPageNumber())
                                    .build();

                                orderBillDto.setUserId(String.valueOf(user.getId()));
                                if(StringUtils.isEmpty(orderBillDto.getOrderId())){
                                    PageInfo info = new PageInfo <>(orderService.queryOrderList(orderBillDto));
                                    t.setData(new MyPageImpl(info.getList(), info.isIsLastPage()));
                                }else {
                                    t.setData(orderService.queryOrderById(orderBillDto));
                                }
                                t.setMessage("查询成功");
                        },
                        (e, t) -> {
                            t.setCode("201");
                            e.printStackTrace();
                            t.setIsSuccess(false);
                            t.setMessage("查询失败");
                        }
                );
    }
}